<?php
namespace app\farm\controller;
use app\AdminRest;
use app\farm\model\ClaimOrder;
use app\farm\model\Farmer;
use app\farm\model\FinanceWater;
use app\farm\model\LandCateText;
use app\farm\model\LandOrder;
use app\farm\model\Machine;
use app\farm\model\Monitor;
use app\farm\model\SendOrder;
use app\farm\model\SystemInfo;
use app\farm\model\User;
use app\farm\model\LandCate;
use app\farm\model\Wallet;
use app\shop\model\StoreGoods;
use longbingcore\wxcore\PayModel;
use longbingcore\wxcore\PospalApi;
use longbingcore\wxcore\PushMsgModel;
use longbingcore\wxcore\WxPay;
use longbingcore\wxcore\YsCloudApi;
use think\App;
use app\farm\model\Farmer as Model;
use think\facade\Db;


class AdminFarmer extends AdminRest
{


    protected $model;

    protected $pay_model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->pay_model = new PayModel($this->payConfig());


    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function farmerList(){


//        $a = 'a:26:{s:10:"gmt_create";s:19:"2022-08-19 11:53:51";s:7:"charset";s:5:"UTF-8";s:12:"seller_email";s:16:"860200049@qq.com";s:7:"subject";s:11:"claim_order";s:4:"sign";s:344:"JeRSkvK1AE/MEbS1l6/djz0HbYiMQ1A7Twt3NcYiLHtwvQiT2n2W6ZDldB4ip9Je35/YqrHkyioLOWgCvUKT11bbIBI6O+wEJzvPJSncXk60CX49G+OVYLjZPNy9379XH7AUfKq+6W2E7CbgjRZTbp/shgXbskJxD7h6sMASsU/NGEDxLLzx5L/+BFOpQrIYJX68dhIiSnXFFSoaCKcuzJ2sXNkwhmPbW1BXTw0uRhfS/t9+PUBPMIH0yUgCD5UP01RaOyMWMRX2uTTLxj7khaEgAVLtJybeKK90UxbWTStn6KZZi+9O/LqjVczg4HOIovjNWGaChlNlf/hjnqtxig==";s:8:"buyer_id";s:16:"2088802696250503";s:14:"invoice_amount";s:4:"0.01";s:9:"notify_id";s:34:"2022081900222115352050501423546615";s:14:"fund_bill_list";s:49:"[{"amount":"0.01","fundChannel":"ALIPAYACCOUNT"}]";s:11:"notify_type";s:17:"trade_status_sync";s:12:"trade_status";s:13:"TRADE_SUCCESS";s:14:"receipt_amount";s:4:"0.01";s:6:"app_id";s:16:"2021003144604274";s:16:"buyer_pay_amount";s:4:"0.01";s:9:"sign_type";s:4:"RSA2";s:9:"seller_id";s:16:"2088441813624801";s:11:"gmt_payment";s:19:"2022-08-19 11:53:52";s:11:"notify_time";s:19:"2022-08-19 11:53:52";s:7:"version";s:3:"1.0";s:12:"out_trade_no";s:25:"2022081911534305900000059";s:12:"total_amount";s:4:"0.01";s:8:"trade_no";s:28:"2022081922001450501410705206";s:11:"auth_app_id";s:16:"2021003144604274";s:14:"buyer_logon_id";s:11:"182****4093";s:12:"point_amount";s:4:"0.00";s:4:"flag";b:1;}';
//
//
//       // $jsApiParameters = $this->pay_model->aliPay('20220722151244011900000119',1,'SchoolShop');
//        $jsApiParameters = $this->pay_model->aliRefund('2022082222001450501413499954',0.01);
//
//        $jsApiParameters = object_array($jsApiParameters);
//
//        dump($jsApiParameters);exit;

//        $a = new PushMsgModel($this->_uniacid);
//
//        $data = ['registration_id'=>['170976fa8a62f84b1ff']];
//
//      //  $data = json_encode($data);
//
//
//        $n = $a->send_pub('1wwww1',$data);
//
//        dump($n);exit;


        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $type = !empty($input['type'])?$input['type']:1;

        $dis[] = ['a.type','=',$type];

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        if(!empty($input['title'])){

            $dis[] = ['a.user_name','like','%'.$input['title'].'%'];

        }

        if(!empty($input['store_name'])){

            $dis[] = ['a.title','like','%'.$input['store_name'].'%'];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];

        }

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['a.user_name','like','%'.$input['name'].'%'];

            $where[] = ['a.phone','like','%'.$input['name'].'%'];
        }

        $data = $this->model->adminDataList($dis,$input['limit'],$where);

        $list = [

            0=>'all',

            1=>'ing',

            2=>'pass',

            4=>'nopass'
        ];

        foreach ($list as $k=> $value){

            $dis_s = [];

            $dis_s[] =['uniacid','=',$this->_uniacid];

            $dis_s[] = ['type','=',$type];

            if(!empty($k)){

                $dis_s[] = ['status','=',$k];

            }else{

                $dis_s[] = ['status','>',-1];

            }

            $data[$value] = $this->model->where($dis_s)->count();

        }

        return $this->success($data);

    }
    /**
     * @author chenniang
     * @DataTime: 2021-03-22 13:35
     * @功能说明:申请认证农场主
     */
    public function farmerAdd(){

        $input = $this->_input;

        $farmer_model = new Farmer();

        $input['uniacid'] = $this->_uniacid;

        $input['status']  = 2;

        $input['is_admin']= 1;

        $input['imgs']    = !empty($input['imgs'])?implode(',',$input['imgs']):'';

        $input['idcard_imgs']  = !empty($input['idcard_imgs'])?implode(',',$input['idcard_imgs']):'';

        $res = $farmer_model->dataAdd($input);

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:58
     * @功能说明:车手详情
     */
    public function farmerInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $user_model = new User();

        $data['nickName'] = $user_model->where(['id'=>$data['user_id']])->value('nickName');

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-07-03 00:15
     * @功能说明:审核(2通过,3取消,4拒绝)
     */
    public function farmerUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $water_model = new FinanceWater();

        $water_model->cashArrival($input['id']);

        $info = $this->model->dataInfo($dis);

        if(!empty($input['status'])&&in_array($input['status'],[2,4,-1])){

            $input['sh_time'] = time();

            if($input['status']==2){

                $this->model->initLandLord($info,$info['uniacid']);
            }

            if($input['status']==-1){

                if($info['cash']>0){

                    $this->errorMsg('农场主还有余额未提现');
                }

                $where[] = ['farmer_id','=',$info['id']];

                $where[] = ['pay_type','in',[2,3,4,5,6]];

                $arr = [

                    'land_order' => new LandOrder(),

                    'claim_order'=> new ClaimOrder(),

                    'send_order' => new SendOrder()
                ];

                foreach ($arr as $value){

                    $find = $value->where($where)->find();

                    if(!empty($find)){

                        $this->errorMsg('农场主还有订单未处理');

                    }

                }
                $wallet_model = new Wallet();

                $wallet = $wallet_model->dataInfo(['farmer_id'=>$info['id'],'status'=>1]);

                if(!empty($wallet)){

                    $this->errorMsg('农场主还有提现未处理');

                }
                //
                $diss = [

                    'farmer_id'  => $input['id'],

                    'cash_status'=> 1,

                    'role_type'  => 1
                ];

                $water = $water_model->where($diss)->sum('price');

                if(!empty($water)&&$water>0){

                    $this->errorMsg('农场主还有冻结金额');

                }

            }

        }

        $data = $this->model->dataUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-30 15:21
     * @功能说明:
     */
    public function farmerSelectList(){

        $input = $this->_param;

        $type = isset($input['type'])?$input['type']:1;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',2];

        $dis[] = ['type','=',$type];

        if(!empty($input['cate_id'])){

            if($input['cate_type']==3){

                $store_goods_model = new StoreGoods();

                $list = $store_goods_model->where(['type'=>2])->where('goods_id','in',$input['cate_id'])->column('store_id');

            }else{

                $cate_text = new LandCateText();

                $list = $cate_text->where(['type'=>$input['cate_type']])->where('cate_id','in',$input['cate_id'])->column('farmer_id');
            }

            if (!empty($list)){
                $dis[] = ['id','in',$list];
            }

        }

        $farmer_model = new Farmer();

        $data = $farmer_model->where($dis)->select()->toArray();

        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-30 17:09
     * @功能说明:农场主提现列表
     */
    public function walletList(){

        $input = $this->_param;

        $dis[] = ['is_show','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['status'])){

            $dis[] = ['status','=',$input['status']];

        }

        if(!empty($input['type'])){

            $dis[] = ['type','=',$input['type']];

        }

        if(!empty($input['order_code'])){

            $dis[] = ['order_code','like','%'.$input['order_code'].'%'];

        }

        $wallet_model = new Wallet();

        $data = $wallet_model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            $user_model = new User();

            foreach ($data['data'] as &$v){

                $v['nickName'] = $user_model->where(['id'=>$v['user_id']])->value('nickName');
            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-09 17:29
     * @功能说明:同意提现打款
     */
    public function walletPass(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $wallet_model = new Wallet();

        $data = $wallet_model->dataInfo($dis);

        if(empty($data)){

            $this->errorMsg('记录未找到');
        }

        if($data['status']!=1){

            $this->errorMsg('记录状态错误');
        }

        $update = [

            'sh_time'   => time(),

            'status'    => 2,

            'online'    => $input['online'],

        ];

        Db::startTrans();

        $res = $wallet_model->dataUpdate(['id'=>$input['id'],'status'=>1],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('打款失败');

        }
        //线上转账
        if($input['online']==1){

            $user_model = new User();

            $openid = $user_model->where(['id'=>$data['user_id']])->value('openid');

            if(empty($openid)){

                return $this->error('用户信息错误，未获取到openid');
            }
            //微信相关模型
            $wx_pay = new WxPay($this->_uniacid);
            //微信提现
            $res    = $wx_pay->crteateMchPay($this->payConfig(),$openid,$data['true_price']);

            if($res['result_code']=='SUCCESS'&&$res['return_code']=='SUCCESS'){

                if(!empty($res['payment_no'])){

                    $wallet_model->dataUpdate(['id'=>$input['id']],['payment_no'=>$res['payment_no']]);
                }

            }else{

                Db::rollback();

                return $this->error(!empty($res['err_code_des'])?$res['err_code_des']:'你还未该权限');

            }

        }

        Db::commit();

        $sys_model = new PushMsgModel($data['uniacid']);

        $sys_model->sendMsg($data,1);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-09 17:43
     * @功能说明:拒绝退款
     */
    public function walletNoPass(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $wallet_model = new Wallet();

        $data = $wallet_model->dataInfo($dis);

        if(empty($data)){

            $this->errorMsg('记录未找到');
        }

        if($data['status']!=1){

            $this->errorMsg('记录状态错误');
        }

        Db::startTrans();

        $update = [

            'sh_time' => time(),

            'status'  => 3,

        ];

        $res = $wallet_model->dataUpdate(['id'=>$input['id'],'status'=>1],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('拒绝失败');

        }

        if($data['type']!=3){

            $water_model = new FinanceWater();
            //添加拒绝提现记录
            $res = $water_model->addWater($data['id'],12,$data['type'],1);

            if($res==0){

                Db::rollback();

                $this->errorMsg('拒绝失败');

            }

            if($data['type']==1){

                $res = $water_model->cashArrival($data['farmer_id']);

            }else{

                $res = $water_model->cashArrival(0,$data['store_id']);
            }

            if($res==0){

                Db::rollback();

                $this->errorMsg('拒绝失败');

            }
        }else{

            $user_model = new User();

            $cash = $data['pay_price'];

            $res = $user_model->where(['id'=>$data['user_id']])->update(['fx_cash'=>Db::Raw("fx_cash+$cash")]);

        }

        Db::commit();

        $sys_model = new PushMsgModel($data['uniacid']);

        $sys_model->sendMsg($data,2);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-12 15:20
     * @功能说明:农场主财务流水
     */
    public function fincanceWaterList(){

        $input = $this->_param;

        $dis[] = ['status','in',[2,3]];

        $dis[] = ['type','=',$input['type']];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['store_name'])){

            $dis[] = ['title','like','%'.$input['store_name'].'%'];
        }

        $start_time = !empty($input['start_time'])?$input['start_time']:'';

        $end_time   = !empty($input['end_time'])?$input['end_time']:'';

        $data = $this->model->dataList($dis,$input['limit']);

        $water_model = new FinanceWater();

        $water_model->cashArrival();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $water_data = $water_model->getDayCash($v['id'],$v['type'],'',$start_time,$end_time,2);
                //当前余额
                $v['cash']  = $water_data['cash'];
                //当前收入
                $v['income_cash'] = $water_data['income_cash'];
                //当前提现
                $v['wallet_cash'] = $water_data['wallet_cash'];

            }

        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-12 16:12
     * @功能说明:农场主财务流水详情
     */
    public function fincanceWaterInfo(){

        $input = $this->_param;

        $water_model = new FinanceWater();

        $water_model->cashArrival();

        if($input['type']==1){

            $where[] = ['farmer_id','=',$input['farmer_id']];

        }else{

            $where[] = ['store_id','=',$input['farmer_id']];
        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $where[] = ['create_time','between',"{$input['start_time']},{$input['end_time']}"];
        }

        $data = $water_model->where($where)->field('create_date')->group('create_date')->paginate($input['limit'])->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //查询各个类型的单日的流水除去余额
                $date_water = $water_model->getDateList($input['farmer_id'],$v['create_date'],$input['type']);

                $v = array_merge($v,$date_water);
                //查询当前余额
                $day_water = $water_model->getDayCash($input['farmer_id'],$input['type'],$v['create_date']);

                $v['cash'] = $day_water['cash'];

            }

        }
        $farmer_model = new Farmer();

        $data['farmer_name'] = $farmer_model->where(['id'=>$input['farmer_id']])->value('title');

        if(!empty($input['start_time'])){

            $data['start_time'] = $input['start_time'];

            $data['end_time']   = $input['end_time'];

        }else{

            $data['start_time'] = $water_model->where($where)->order('id')->value('create_date');

            $data['start_time'] = strtotime($data['start_time']);

            $data['end_time']   = $water_model->where($where)->order('id desc')->value('create_date');

            $data['end_time']   = strtotime($data['end_time']);

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-18 10:07
     * @功能说明:平台统计详情
     */
    public function statisticsCash(){

        $input = $this->_param;

        $water_model = new FinanceWater();

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }
        //总收入
        $data['cash']['total_cash'] = $water_model->where($dis)->where(['add'=>1])->where('type','not in',[12])->sum('price');
        //商城收入
        $data['cash']['shop_cash']  = $water_model->where($dis)->where('type','in',[8,16])->sum('price');
        //土地收入
        $data['cash']['land_cash']  = $water_model->where($dis)->where(['type'=>3])->sum('price');
        //认养收入
        $data['cash']['claim_cash'] = $water_model->where($dis)->where(['type'=>1])->sum('price');
        //养殖收入
        $data['cash']['breed_cash'] = $water_model->where($dis)->where(['type'=>6])->sum('price');
        //配送订单
        $data['cash']['send_cash']  = $water_model->where($dis)->where('type','in',[14,10])->sum('price');
        //退款
        $data['cash']['refund_cash']= $water_model->where($dis)->where('type','in',[2,4,9,11,13,17])->sum('price');

        $total = ($input['end_time']-$input['start_time'])/86400;

        $total = ceil($total);

        $day = 0;

        $arr = [

            '1' => 'claim_cash',

            '3' => 'land_cash',

            '8' => 'shop_cash',
            //商城订单运费
            '16'=> 'shop_car_cash',

        ];
        //每页条数
        $list['per_page'] = 30;
        //当前页
        $list['current_page'] = $input['page'];
        //总条数
        $list['total'] = $total;
        //最后一页
        $list['last_page'] = ceil($total/$list['per_page']);

        $total -= $list['per_page']*($list['current_page']-1);

        $all = $total>$list['per_page']?$list['per_page']:$total;

        while ($day<$all){

            $date = $input['start_time']+$day*86400+$list['per_page']*($list['current_page']-1)*86400;

            foreach ($arr as $ks=>$vs){

                $where = [

                    'create_date' => date('Y-m-d',$date),

                    'type'        => $ks
                ];

                $list['data'][$day][$vs] = $water_model->where($where)->sum('price');
            }

            $list['data'][$day]['shop_cash']+= $list['data'][$day]['shop_car_cash'];

            $list['data'][$day]['date'] = date('m-d',$date);

            $day ++;

        }

        $data['list'] = $list;

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-18 11:03
     * @功能说明:用户统计详情
     */
    public function statisticsUser(){

        $input = $this->_param;

        $user_model = new User();

        $dis[] = ['uniacid','=',$this->_uniacid];
        //用户总数量
        $data['total_count'] = $user_model->where($dis)->count();

        $year = date('Y');

        $last_year = $year-1;

        $where['year'] = $last_year;

        $now_where['year'] = $year;

        switch ($input['date']){

            case 1:
                $time      = 'today';

                $last_time = 'yesterday';
                //去年今日
                $where['create_date'] = date('Y-m-d',strtotime("-1 year"));

                $now_where['create_date'] = date('Y-m-d',time());

                break;
            case 2:
                $time      = 'week';

                $last_time = 'last week';

                $where['week'] = date('W');

                $now_where['week'] = date('W');

                break;
            case 3:
                $time      = 'month';

                $last_time = 'last month';

                $where['month'] = date('m');

                $now_where['month'] = date('m');

                break;
            case 4:
                $time      = 'year';

                $last_time = 'last year';

                break;

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }
        //新增人数
        $data['new_count'] = $user_model->where($dis)->count();
        //去年当日/当周/当月用户数量
        $last_time_count = $user_model->where($where)->count();

        $now_time_count  = $user_model->where($now_where)->count();
        //同比
        $data['t_balance'] = $last_time_count>0?round(($now_time_count-$last_time_count)/$last_time_count*100,2):0;

        $last_count = $user_model->whereTime('create_time',$last_time)->count();

        $now_count  = $user_model->whereTime('create_time',$time)->count();

//        dump($last_count,$now_count);exit;
        //环比
        $data['h_balance'] = $last_count>0?round(($now_count-$last_count)/$last_count*100,2):0;

        $total = ($input['end_time']-$input['start_time'])/86400;

        $day = 0;
        //每页条数
        $list['per_page'] = 30;
        //当前页
        $list['current_page'] = $input['page'];
        //总条数
        $list['total'] = $total;
        //最后一页
        $list['last_page'] = ceil($total/$list['per_page']);

        $total -= $list['per_page']*($list['current_page']-1);

        $all = $total>$list['per_page']?$list['per_page']:$total;

        while ($day<$all){

            $date = $input['start_time']+$day*86400+$list['per_page']*($list['current_page']-1)*86400;

            $list['data'][$day]['week'] = changeWeek(date('w',$date));

            $where = [

                'create_date' => date('Y-m-d',$date),

            ];

            $list['data'][$day]['count'] = $user_model->where($where)->count();

            $list['data'][$day]['date']  = date('m-d',$date);;

            $day++;

        }

        $data['list'] = $list;

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-21 11:00
     * @功能说明:仪器列表
     */
    public function machineList(){

        $input = $this->_param;

        $mac_model = new Machine();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['farmer_id'])){

            $dis[] = ['farmer_id','=',$input['farmer_id']];

        }

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }

        $data = $mac_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2022-02-21 11:00
     * @功能说明:仪器列表
     */
    public function machineSelect(){

        $input = $this->_param;

        $mac_model = new Machine();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        if(!empty($input['farmer_id'])){

            $dis[] = ['farmer_id','=',$input['farmer_id']];

        }

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }

        $data = $mac_model->where($dis)->order('status desc,id desc')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-21 11:00
     * @功能说明:仪器详情
     */
    public function machineInfo(){

        $input = $this->_param;

        $mac_model = new Machine();

        $dis = [

            'id' => $input['id']
        ];

        $data = $mac_model->dataInfo($dis);

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2022-02-21 11:00
     * @功能说明:添加仪器
     */
    public function machineAdd(){

        $input = $this->_input;

        $mac_model = new Machine();

        $input['uniacid'] = $this->_uniacid;

        $data = $mac_model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-21 11:00
     * @功能说明:编辑仪器
     */
    public function machineUpdate(){

        $input = $this->_input;

        $mac_model = new Machine();

        $dis = [

            'id' => $input['id']
        ];

        $data = $mac_model->dataUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-03-07 17:40
     * @功能说明:店铺下拉框
     */
    public function storeSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',2];

        $dis[] = ['type','=',2];

        if(!empty($input['goods_id'])){

            $goods_store_model = new StoreGoods();

            $store_id = $goods_store_model->where(['goods_id'=>$input['goods_id']])->column('store_id');

            $dis[] = ['id','in',$store_id];

        }

        $farmer_model = new Farmer();

        $data = $farmer_model->where($dis)->order('id desc')->select()->toArray();

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2022-04-11 14:22
     * @功能说明:监控列表
     */
    public function monitorList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['farmer_id'])){

            $dis[] = ['farmer_id','=',$input['farmer_id']];

        }

        $monitor_model = new Monitor();

        $data = $monitor_model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['farmer_name'] = $this->model->where(['id'=>$v['farmer_id']])->value('title');

            }
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-04-11 14:22
     * @功能说明:监控列表
     */
    public function monitorSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['farmer_id'])){

            $dis[] = ['farmer_id','=',$input['farmer_id']];

        }

        $monitor_model = new Monitor();

        $data = $monitor_model->where($dis)->order('id desc')->select()->toArray();

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2022-04-11 14:25
     * @功能说明:添加监控
     */
    public function monitorAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $monitor_model = new Monitor();

        $res = $monitor_model->dataAdd($input);

        return $this->success($res);

    }

    /**
     * @author chenniang
     * @DataTime: 2022-04-11 14:25
     * @功能说明:添加监控
     */
    public function monitorInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $monitor_model = new Monitor();

        $res = $monitor_model->dataInfo($dis);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-04-11 14:25
     * @功能说明:添加监控
     */
    public function monitorUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $monitor_model = new Monitor();

        $res = $monitor_model->dataUpdate($dis,$input);

        return $this->success($res);

    }






}
